﻿namespace TestTool.GUI
{
    using System;
    using System.Net.Sockets;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using System.Xml.Serialization;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Data;
    using TestTool.Tests.Common.Discovery;
    using TestTool.Tests.Common.TestEngine;
    using TestTool.Tests.Definitions.Data;
    using TestTool.Tests.Engine;

    [XmlRoot("Parameters")]
    public class SerializableTestingParameters
    {
        public TestSuiteParameters GetTestSuiteParameters()
        {
            TestSuiteParameters parameters = new TestSuiteParameters();
            if (this.Device != null)
            {
                parameters.Address = this.Device.DeviceServiceAddress;
            }
            if (string.IsNullOrEmpty(parameters.Address))
            {
                Console.WriteLine("Mandatory parameters (Device address) not defined!");
                return null;
            }
            Timeouts timeouts = new Timeouts();
            TestSettings settings = new TestSettings();
            if (this.TestParameters != null)
            {
                parameters.MessageTimeout = (this.TestParameters.MessageTimeout != 0) ? this.TestParameters.MessageTimeout : timeouts.Message;
                parameters.RebootTimeout = (this.TestParameters.RebootTimeout != 0) ? this.TestParameters.RebootTimeout : timeouts.Reboot;
                parameters.RecoveryDelay = this.TestParameters.TimeBetweenRequests;
                parameters.TimeBetweenTests = this.TestParameters.TimeBetweenTests;
                parameters.OperationDelay = (this.TestParameters.OperationDelay != 0) ? this.TestParameters.OperationDelay : settings.OperationDelay;
                parameters.UserName = this.TestParameters.UserName;
                parameters.Password = this.TestParameters.Password;
                parameters.EnvironmentSettings = new TestTool.Tests.Common.TestEngine.EnvironmentSettings();
                parameters.EnvironmentSettings.DefaultGateway = this.TestParameters.DefaultGatewayIpv4;
                parameters.EnvironmentSettings.DefaultGatewayIpv6 = this.TestParameters.DefaultGatewayIpv6;
                parameters.EnvironmentSettings.DnsIpv4 = this.TestParameters.DnsIpv4;
                parameters.EnvironmentSettings.DnsIpv6 = this.TestParameters.DnsIpv6;
                parameters.EnvironmentSettings.NtpIpv4 = this.TestParameters.NtpIpv4;
                parameters.EnvironmentSettings.NtpIpv6 = this.TestParameters.NtpIpv6;
                if (!string.IsNullOrEmpty(this.TestParameters.Address))
                {
                    foreach (NetworkInterfaceDescription description in DiscoveryHelper.GetNetworkInterfaces())
                    {
                        if (description.IP.ToString() == this.TestParameters.Address)
                        {
                            parameters.NetworkInterfaceController = description;
                            break;
                        }
                    }
                    if (parameters.NetworkInterfaceController != null)
                    {
                        bool flag = (parameters.NetworkInterfaceController.IP != null) && (parameters.NetworkInterfaceController.IP.AddressFamily == AddressFamily.InterNetworkV6);
                        parameters.CameraIP = DiscoveryUtils.GetIP(this.Device.DeviceIP, flag);
                    }
                }
                parameters.UseEmbeddedPassword = this.TestParameters.UseEmbeddedPassword;
                parameters.Password1 = this.TestParameters.Password1;
                parameters.Password2 = this.TestParameters.Password2;
                parameters.SecureMethod = !string.IsNullOrEmpty(this.TestParameters.SecureMethod) ? this.TestParameters.SecureMethod : settings.SecureMethod;
                parameters.PTZNodeToken = this.TestParameters.PTZNodeToken;
                parameters.VideoSourceToken = this.TestParameters.VideoSourceToken;
                parameters.EventTopic = this.TestParameters.EventTopic;
                parameters.SubscriptionTimeout = (this.TestParameters.SubscriptionTimeout != 0) ? this.TestParameters.SubscriptionTimeout : settings.SubscriptionTimeout;
                parameters.TopicNamespaces = this.TestParameters.TopicNamespaces;
                parameters.RelayOutputDelayTimeMonostable = (this.TestParameters.RelayOutputDelayTime != 0) ? this.TestParameters.RelayOutputDelayTime : settings.RelayOutputDelayTimeMonostable;
            }
            return parameters;
        }

        [XmlArrayItem(ElementName="Value")]
        public XmlElement[] Advanced { get; set; }

        public DeviceParameters Device { get; set; }

        public TestTool.GUI.Output Output { get; set; }

        public TestTool.GUI.SessionInfo SessionInfo { get; set; }

        public TestTool.GUI.TestParameters TestParameters { get; set; }
    }
}

